package com.dongdongshop.config;

import com.dongdongshop.pojo.TbAdmin;
import com.dongdongshop.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public class LoginRealm extends AuthorizingRealm {
    @DubboReference
    private AdminService adminService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        System.out.println("授权方法");

        return null;
    }

    //shiro 的登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("2222");
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        TbAdmin admin = adminService.selectByName(token.getUsername());
        if (admin == null){
            return null;
        }
        //1.登录后存入 session的对象,2.密码自动校验,3.可以理解为用户名
        return new SimpleAuthenticationInfo(admin,admin.getPwd(), ByteSource.Util.bytes(admin.getSalt()),"");
    }
}
